package com.dragon.app.ykd.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.antifraud.entity.AcLinkmanMobileRecordTemp;
import com.dragon.app.commons.utils.UserUtils;
import com.fintech.common.constant.ErrorCode;
import com.fintech.common.enums.ReturnCode;
import com.fintech.common.vo.FintechResult;
import com.fintech.third.service.BurialPointService;
import com.fintech.user.entity.RealNameAuthentication;
import com.fintech.user.entity.User;
import com.fintech.user.service.RealNameAuthenticationService;
import com.marvels.common.utils.MarvelsLogUtil;

import cn.hutool.core.util.StrUtil;

/**
 * 埋点数据
 * @author liuxy
 *
 */
@Service("burialPointAppService")
public class BurialPointAppServiceImpl implements BurialPointAppService{
	@Autowired
	private BurialPointService burialPointService;
	@Autowired
	private RealNameAuthenticationService realNameAuthenticationService;

	/**
	 * 记录通话记录
	 * 
	 * @param map
	 * @param accessToken
	 * @param idFintechUmUser
	 * @return
	 */
	@Override
	public FintechResult addCallRecords(Map<String, Object> map, String accessToken,
			String idFintechUmUser) {
		MarvelsLogUtil.getInstance().info("===============addCallRecords is begine===============");
		try {
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			String idNo = realNameAuthentication.getIdentityCardNo();
			String loanId = map.get("loanId") + "";
			Set<AcLinkmanMobileRecordTemp> set = new HashSet<AcLinkmanMobileRecordTemp>();
			List<Map<String, String>> list = (List<Map<String, String>>) map.get("dataList");
			if (list != null && list.size() > 0) {
				MarvelsLogUtil.getInstance().info("通话记录获取成功");
				for (Map<String, String> data : list) {
					AcLinkmanMobileRecordTemp recordInfo = new AcLinkmanMobileRecordTemp();
					data.put("idNo", idNo);
					data.put("loanId", loanId);
					
					String number = data.get("peerNumber");
					if(StringUtils.isNotEmpty(number)){
						number=number.replaceAll("[^0-9]+", "");
						recordInfo.setLoanId(loanId);
						recordInfo.setCustIdCard(idNo);
						recordInfo.setName(data.get("peerName"));
						recordInfo.setMobile(number);
						recordInfo.setCreatedBy("sys");
						recordInfo.setCreatedDate(new Date());
						recordInfo.setUpdatedBy("sys");
						recordInfo.setUpdatedDate(new Date());
						recordInfo.setType("2");
						set.add(recordInfo);
					}
				}
				burialPointService.addCallRecords(list);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("addCallRecords异常", e);
			String errMsg = StringUtils.isEmpty(e.getMessage()) ? "系统异常" : e.getMessage();
			return FintechResult.getResult(ErrorCode.FAIL, errMsg, null);
		}
		return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", null);
	}

	/**
	 * 记录短信记录
	 * 
	 * @param map
	 * @param accessToken
	 * @param idFintechUmUser
	 * @return
	 */
	@Override
	public FintechResult addMsgRecords(Map<String, Object> map, String accessToken,
			String idFintechUmUser) {
		MarvelsLogUtil.getInstance().info("===============addMsgRecords is begine===============");
		try {
			User user = UserUtils.getUser(accessToken, idFintechUmUser);
			if (user == null) {
				return FintechResult.getResult(ReturnCode.tokenInvalid.getErrorCode(), "登录超时，请重新登录！", null);
			}
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			String idNo = realNameAuthentication.getIdentityCardNo();
			String loanId = map.get("loanId") + "";
			List<Map<String, String>> list = (List<Map<String, String>>) map.get("dataList");
			if (list != null && list.size() > 0) {
				MarvelsLogUtil.getInstance().info("短信记录获取成功");
				for (Map<String, String> data : list) {
					data.put("idNo", idNo);
					data.put("loanId", loanId);
					
					try {
						if(data.get("messageContent") != null && data.get("messageContent").length() >= 1000){
							data.put("messageContent", StrUtil.sub(data.get("messageContent"), 0, 1000));
						}
					} catch (Exception e) {
						MarvelsLogUtil.getInstance().error("短信截取异常，但是不影响哦", e);
					}
				}
				burialPointService.addMsgRecords(list);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("addMsgRecords异常", e);
			String errMsg = StringUtils.isEmpty(e.getMessage()) ? "系统异常" : e.getMessage();
			return FintechResult.getResult(ErrorCode.FAIL, errMsg, null);
		}
		return FintechResult.getResult(ErrorCode.SUCCESS, "操作成功", null);
	}

}
